package com.pantech.app.call_log_enabler;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
import android.telephony.TelephonyManager;
import android.view.Gravity;
import android.widget.Toast;

public class CallStateReceiver extends BroadcastReceiver {
	
	public static boolean mRunning = false;
	public static Toast m_Toast;

	@Override
	public void onReceive(Context context, Intent intent) {
		String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
		
		if(TelephonyManager.EXTRA_STATE_RINGING.equals(state)
			|| TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state)) {
			UserSharedPreference mUserSharedPreference = new UserSharedPreference(context);
			if(mUserSharedPreference.getBoolean_pref(UserSharedPreference.pref_IS_FIELDTEST_MODE)
				&& !mUserSharedPreference.getBoolean_pref(UserSharedPreference.pref_CALLLOG_ONGOING)) {
				if(m_Toast == null) {
					m_Toast = Toast.makeText(context, "\n     < !! Log is not enabled !! >     \n", Toast.LENGTH_SHORT);
					m_Toast.setGravity(Gravity.TOP, 0, 0);
					new Thread(new Runnable() {
						@Override
						public void run() {
							mRunning = true;
							while(mRunning) {
								m_Toast.show();
								//mHandler.sendEmptyMessage(0);
								SystemClock.sleep(1 * 1000);
							}
						}}).start();
				}
			}
		}else if(TelephonyManager.EXTRA_STATE_IDLE.equals(state)) {
			mRunning = false;
			m_Toast = null;
		}
	}
}
